package com.bjwen;

public class Complex {
    private double real;
    private double image;

    Complex(double real, double image) {
        this.real = real;
        this.image = image;
    }

    Complex(double real) {
        this.real = real;
        this.image = 0;
    }

    Complex(Complex c) {
        this.real = c.getReal();
        this.image = c.getImage();
    }

    public Complex add(Complex c, boolean addToNewCom) {
        if (addToNewCom) {
            return new Complex(real + c.getReal(), image + c.getImage());
        } else {
            this.real += c.getReal();
            this.image += c.getImage();
            return this;
        }
    }

    public Complex minus(Complex c, boolean minusToNewCom) {
        if (minusToNewCom) {
            return new Complex(real - c.getReal(), image - c.getImage());
        } else {
            this.real -= c.getReal();
            this.image -= c.getImage();
            return this;
        }
    }

    public Complex multiply(Complex c, boolean toNewCom) {
        if (toNewCom) {
            return new Complex(real * c.getReal() - c.getImage() * image, real * c.getImage() + image * c.getReal());
        }
        double a, b;
        a = real;
        b = image;
        real = a * c.getReal() - c.getImage() * b;
        image = a * c.getImage() + c.getReal() * b;
        return this;
    }

    public Complex conjugate(boolean toNewCom) {
        if (toNewCom) {
            return new Complex(real, image * -1);
        }
        double temp = real;
        real = image;
        image = temp;
        return this;
    }

    public double getReal() {
        return real;
    }

    public double getImage() {
        return image;
    }


    @Override
    public String toString() {
        return String.format("%.4f", real) + "+j" + String.format("%.4f", image);
    }
}
